package gui;

import gui.MainFrame.Screens;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import data.GameState;
import data.IGameDataReadOnly;
import data.IPlayerReadOnly;
import data.Player;
import eolympics.Game;

public class StartGameScreen
        extends ScreenParent
{
    private static final long                serialVersionUID = 8668282186511919491L;
    private final IMainFrameScreenController mMainFrameScreenController;
    private final JPanel                     mPlayersPanel    = new JPanel();

    private final Game                       mGame;

    public StartGameScreen(IMainFrameScreenController pIMainFrameScreenController, final Game pGame)
    {
        super();

        mGame = pGame;
        mMainFrameScreenController = pIMainFrameScreenController;

        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        add(topPanel, BorderLayout.NORTH);

        JButton startGameButton = new JButton("Start");
        topPanel.add(startGameButton);

        ActionListener startListener = new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                startGame();
            }
        };
        startGameButton.addActionListener(startListener);

        JButton cancelButton = new JButton("Cancel");
        cancelButton.setName("cancel");
        topPanel.add(cancelButton);

        ActionListener cancelListener = new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                cancelGame();
            }
        };
        cancelButton.addActionListener(cancelListener);

        JScrollPane playersScrollPane = new JScrollPane(mPlayersPanel);
        add(playersScrollPane, BorderLayout.CENTER);

        mPlayersPanel.setLayout(new BoxLayout(mPlayersPanel, BoxLayout.PAGE_AXIS));
    }

    @Override
    public void init()
    {
        List<IPlayerReadOnly> players = mGame.getClientGameData().getReadOnlyPlayerList();
        refreshPlayersPanel(players);
    }

    private void refreshPlayersPanel( final List<IPlayerReadOnly> pPlayerList )
    {
        mPlayersPanel.removeAll();

        for( IPlayerReadOnly currentPlayer : pPlayerList )
        {
            JPanel playerPanel = new JPanel();
            playerPanel.setLayout(new FlowLayout());
            playerPanel.add(new JLabel("Player: " + currentPlayer.getName()));
            mPlayersPanel.add(playerPanel);

            ActionListener removeAiListener = new ActionListener()
            {
                @Override
                public void actionPerformed( ActionEvent pActionEvent )
                {
                    JButton removeButton = (JButton) pActionEvent.getSource();
                    String playerName = removeButton.getName();
                    mGame.removeAiPlayerFromServer(playerName);
                }
            };

            if( mGame.isServerRunning() )
            {
                if( !currentPlayer.isAiPlayer() )
                {
                    continue;
                }
                JButton removeAiButton = new JButton("remove AI");
                removeAiButton.addActionListener(removeAiListener);
                removeAiButton.setName(currentPlayer.getName());
                playerPanel.add(removeAiButton);
            }
        }

        if( mGame.isServerRunning() )
        {
            JButton addAiButton = new JButton("add AI");
            ActionListener addAiListener = new ActionListener()
            {
                public void actionPerformed( ActionEvent e )
                {
                    addAi();
                }
            };
            addAiButton.addActionListener(addAiListener);
            mPlayersPanel.add(addAiButton);
        }

        revalidate();
        repaint();
    }

    private void addAi()
    {
        Player aiPlayer = new Player();
        aiPlayer.setName("ai");

        mGame.addAiPlayerToServer(aiPlayer);
    }

    private void cancelGame()
    {
        mGame.stopGame();
    }

    private void startGame()
    {
    }

    @Override
    public void playerListChanged( final IGameDataReadOnly pGameData )
    {
        refreshPlayersPanel(pGameData.getReadOnlyPlayerList());
    }

    @Override
    public void gameStateChanged( final IGameDataReadOnly pGameData )
    {
        if( pGameData.getGameState() == GameState.NOT_RUNNING )
        {
            mMainFrameScreenController.changeScreen(Screens.HOME_SCREEN);
        }
    }

}
